# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#

update_batch_button_status = ->
    if page_perms.check
        $('button#batch_check').removeClass('has_clicked').text('批量复核')
    if page_perms.disable
        $('button#batch_disable').removeClass('has_clicked').text('批量禁用')
        $('button#batch_enable').removeClass('has_clicked').text('批量启用')
    if page_perms.delete
        $('button#batch_delete').removeClass('has_clicked').text('批量删除')

    checked_boxes = $('tbody .checkbox.checked#batch')
    if checked_boxes.length == 0
        $('button#batch_check').addClass('disabled')
        $('button#batch_disable').addClass('disabled')
        $('button#batch_enable').addClass('disabled')
        $('button#batch_delete').addClass('disabled')
        return

    batch_checkable = true
    batch_disable = true
    batch_enable = true
    batch_deletable = true
    batch_deletable_ex = true

    # 判断当前的选择是否可以执行批复核操作
    if page_perms.batch and page_perms.check
        for box in checked_boxes
            if not $(box).data('batch-checkable')
                batch_checkable = false
                break
    else
        batch_checkable = false

    # 判断当前的选择是否可以执行批禁用操作
    if page_perms.batch and page_perms.disable
        for box in checked_boxes
            if not $(box).data('batch-disable')
                batch_disable = false
                break
    else
        batch_disable = false

    # 判断当前的选择是否可以执行批启用操作
    if page_perms.batch and page_perms.disable
        for box in checked_boxes
            if not $(box).data('batch-enable')
                batch_enable = false
                break
    else
        batch_enable = false

    # 判断当前的选择是否可以执行批删除操作
    if page_perms.batch and page_perms.delete
        for box in checked_boxes
            if not $(box).data('batch-deletable')
                batch_deletable = false
                break
    else
        batch_deletable = false

    # 判断当前的选择是否可以执行批删除操作(ex)
    if page_perms.batch and page_perms.delete
        for box in checked_boxes
            if not $(box).data('batch-deletable-ex')
                batch_deletable_ex = false
                break
    else
        batch_deletable_ex = false

    if batch_checkable
        $('button#batch_check').removeClass('disabled')
    else
        $('button#batch_check').addClass('disabled')
    if batch_disable
        $('button#batch_disable').removeClass('disabled')
    else
        $('button#batch_disable').addClass('disabled')
    if batch_enable
        $('button#batch_enable').removeClass('disabled')
    else
        $('button#batch_enable').addClass('disabled')
    if batch_deletable != batch_deletable_ex
        $('button#batch_delete').removeClass('disabled')
    else
        $('button#batch_delete').addClass('disabled')


$ ->
    # 查询外联终端的商户信息
    $('a#mapto_term_info').click (evt) ->
        evt.preventDefault()
        $.post '/terms/query/mapto_term_merch', {
            mapto_term: $(this).data('mapto-term')
        }, (resp) ->
            return if not resp.succ
            if not resp.record
                return alert '未查询到对应的商户信息'

            $('a#mapto_term_info').text "｜ " +
                "#{resp.record.mer_name} ｜ " +
                "#{resp.record.mer_no} ｜ " +
                "#{if resp.record.inside then '平台商户' else '外联商户'} ｜ " +
                "#{resp.record.mer_chnl}"

    # 转到修改页面
    $('i#input').click ->
        uuid = $(this).data('uuid')
        window.location = "/terms/input/#{uuid}"

    # 转到复核界面
    $('i#check').click ->
        uuid = $(this).data('uuid')
        action = $(this).data('action')
        window.location = "/terms/check/#{action}/#{uuid}"

    # 批量处理按钮事件
    $('button#batch').click ->
        $('th#status, td#status').hide()
        $('th#batch, td#batch').show()
        $('form#search_form').find('.input').addClass('disabled')
        $('tfoot').find('*').addClass('disabled').attr('disabled', 'disabled')
        $('.buttons#primary').hide('fast')
        $('.buttons#secondary').show('fast')

    # 退出批量处理按钮事件
    $('button#batch_exit').click ->
        $('th#batch, td#batch').hide()
        $('th#status, td#status').show()
        $('form#search_form').find('.input').removeClass('disabled')
        $('tfoot').find('*').removeClass('disabled').removeAttr('disabled')
        $('.buttons#secondary').hide('fast')
        $('.buttons#primary').show('fast')

    # 批量处理模式下，选择框改变时处理
    $('.checkbox#batch').each ->
        $(this).checkbox
            onChecked: ->
                update_batch_button_status()
            onUnchecked: ->
                update_batch_button_status()

    # 导出按钮，将当前查询关键字引入到导出页面
    $('button#export').click ->
        keyword = $('#search_form input[name=keyword]').val()
        window.location = "/terms/export?keyword=#{keyword}"

    # 重置所有批量处理按钮为默认状态
    reset_batch_buttons = ->
        $('button#batch_check').removeClass('has_clicked').text('批量复核')
        $('button#batch_disable').removeClass('has_clicked').text('批量禁用')
        $('button#batch_enable').removeClass('has_clicked').text('批量启用')
        $('button#batch_delete').removeClass('has_clicked').text('批量删除')

    # 批量复核
    $('button#batch_check').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量复核')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/terms/batch/check', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_check').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量复核')

    # 批量禁用
    $('button#batch_disable').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量禁用')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/terms/batch/disable', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_disable').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量禁用')

    # 批量启用
    $('button#batch_enable').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量启用')
            records = []
            $('tbody .checkbox.checked#batch').each ->
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            $.post '/terms/batch/enable', {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_enable').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量启用')

    # 批量删除
    $('button#batch_delete').click ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('批量删除')
            records = []
            deleteable_ex = false
            $('tbody .checkbox.checked#batch').each ->
                deleteable_ex = $(this).data('batch-deletable-ex')
                records.push
                    serial: $(this).data('serial')
                    uuid: $(this).data('uuid')
            $(this).addClass 'loading'
            url = '/terms/batch/delete'
            if deleteable_ex then url += "_ex"
            $.post url, {
                records: JSON.stringify(records)
            }, (resp) ->
                $('button#batch_delete').removeClass 'loading'
        else
            reset_batch_buttons()
            $(this).addClass('has_clicked').text('确认批量删除')

    # 解密终端主密钥
    $('.tmk.decrypt').click (evt) ->
        evt.preventDefault()
        $this = $(this)
        if $this.data('decrypted')
            $.post '/secure/encrypt_ede', {
                key: $this.text()
            }, (resp) ->
                return if not resp.succ
                $this.data('decrypted', false).text resp.key
                $this.css('color', '')
        else
            $.post '/secure/decrypt_ede', {
                key: $this.text()
            }, (resp) ->
                return if not resp.succ
                $this.data('decrypted', true).text resp.key
                $this.css('color', 'red')
